package cn.stylefeng.guns.modular.manager.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.service.ArchiveDictService;
import cn.stylefeng.guns.modular.basic.service.CommonService;
import cn.stylefeng.guns.modular.manager.entity.ArchiveMaterials;
import cn.stylefeng.guns.modular.manager.entity.ArchiveMaterialsAttach;
import cn.stylefeng.guns.modular.manager.mapper.ArchiveMaterialsMapper;
import cn.stylefeng.guns.modular.manager.model.params.ArchiveMaterialsParam;
import cn.stylefeng.guns.modular.manager.model.result.ArchiveMaterialsResult;
import cn.stylefeng.guns.modular.manager.service.ArchiveMaterialsAttachService;
import cn.stylefeng.guns.modular.manager.service.ArchiveMaterialsService;
import cn.stylefeng.guns.sys.core.constant.state.YesNo;
import cn.stylefeng.guns.utils.ServiceAssert;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 档案资料信息表 服务实现类
 * </p>
 *
 * @author blesslp
 * @since 2019-11-20
 */
@Service
public class ArchiveMaterialsServiceImpl extends ServiceImpl<ArchiveMaterialsMapper, ArchiveMaterials> implements ArchiveMaterialsService {

    @Autowired
    private ArchiveMaterialsAttachService archiveMaterialsAttachService;
    @Autowired
    private ArchiveDictService archiveDictService;
    @Autowired
    private CommonService commonService;

    @Override
    public void add(ArchiveMaterialsParam param) {
        ArchiveMaterials entity = getEntity(param);
        //默认有效
        entity.setSfyx(YesNo.YES.getCode());
        this.save(entity);
    }

    @Override
    @Transactional
    public void delete(ArchiveMaterialsParam param) {
        final Serializable key = getKey(param);
        boolean isSuccess = this.removeById(key);
        final String TIPS = "删除失败";
        ServiceAssert.assetTrue(isSuccess, TIPS);
        //在删除关联的附件
        this.archiveMaterialsAttachService.lambdaUpdate()
                .eq(ArchiveMaterialsAttach::getDazlid, key)
                .remove();
    }

    @Override
    public void update(ArchiveMaterialsParam param) {
        ArchiveMaterials oldEntity = getOldEntity(param);
        ArchiveMaterials newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public ArchiveMaterialsResult findBySpec(ArchiveMaterialsParam param) {
        return null;
    }

    @Override
    public List<ArchiveMaterialsResult> findListBySpec(ArchiveMaterialsParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(ArchiveMaterialsParam param) {
        Page pageContext = getPageContext();
        if (ToolUtil.isNotEmpty(param.getZllx())) {
            final List<String> childDictWithParent = this.archiveDictService.getChildDictWithParent(param.getZllx());
            childDictWithParent.add(0, param.getZllx());
            if (ToolUtil.isNotEmpty(childDictWithParent)) {
                param.setZllxArr(childDictWithParent);
            }
        }

        if (ToolUtil.isNotEmpty(param.getSsbmid())) {
            final List<String> childDepts = this.commonService.getChildDepts(Long.parseLong(param.getSsbmid()));
            childDepts.add(0, param.getSsbmid());
            if (ToolUtil.isNotEmpty(childDepts)) {
                param.setSsbmidArr(childDepts);
            }
        }

        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(ArchiveMaterialsParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private ArchiveMaterials getOldEntity(ArchiveMaterialsParam param) {
        return this.getById(getKey(param));
    }

    private ArchiveMaterials getEntity(ArchiveMaterialsParam param) {
        ArchiveMaterials entity = new ArchiveMaterials();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
